<!--
 * @Author: zjf
 * @Date: 2023-11-28 19:36:50
 * @LastEditors: zjf
 * @LastEditTime: 2023-12-16 18:57:08
 * @Description: 基本信息
-->
<template>
  <l-layout class="l-tab-page" :top="queryHeight + 12">
    <template #top>
      <!-- l-fill-options 组件用法 -->
      <l-fill-options :queryItems="queryItems" :queryData="queryData" @change="handleSearch"
        @changeHeight="(h) => queryHeight = h"></l-fill-options>

    </template>

    <l-panel style="padding-top:0;">
      <template #toolLeft>
        <div class="l-panel--item">
        </div>
        <div class="l-panel--item">
          <l-excel-btns></l-excel-btns>
        </div>
      </template>
      <template #toolRight>
        <l-tool-btns :hasAdd="false" :hasTableSetting="true" @setting="lr_handleSettingTable"> </l-tool-btns>
      </template>

      <l-table ref="mainTable" :loading="tableLoading" :columns="columns" :dataSource="tableData" :isPage="true"
        :pageTotal="tableTotal" :tablePage.sync="tableCurrentPage" @loadPageData="turnTablePage">
        <!-- 项目名称 -->
        <template v-slot:projectName="scope">
          <a slot="reference" @click="handleDetails(0, scope.row)">{{ scope.row.projectName || '---' }}</a>
        </template>
        <!-- 建设起止时间 -->
        <template v-slot:projectStartEndDate="scope">
          <span>{{ scope.row.prjStartYear }}年 － </span>
          <span>{{ scope.row.prjEndYear }}年</span>
        </template>
        <!-- 是否属于娄江新城 -->
        <template v-slot:isLjxc="scope">
          <span>
            {{ scope.row.isLjxc ? '是' : '否' }}
          </span>
        </template>
        <!-- 是否重点项目 -->
        <template v-slot:isKeyPrj="scope">
          <span>
            {{ scope.row.isKeyPrj ? '是' : '否' }}
          </span>
        </template>
        <template v-for="(col, index) in lr_tableColumnsByToLabel(columns)" v-slot:[col.prop]="scope">
          <l-value-to-label :value="scope.row[col.prop]" :type="col.dataType" :code="col.dataCode"
            :valueKey="col.valueKey" :labelKey="col.labelKey" :options="col.options" :format="col.format"
            :setting="col.setting" :key="index"></l-value-to-label>
        </template>
        <l-table-btns :btns="tableBtns" :filterBtns="filterBtns" @click="lr_handleTableBtnClick"
          :btnNum="6"></l-table-btns>
      </l-table>
    </l-panel>

    <l-project-details ref="ProjectDetails"></l-project-details>

    <DialogForm ref="DialogForm" @refresh="loadTableData"></DialogForm>

    <l-stage-add ref="StageAdd"></l-stage-add>

  </l-layout>
</template>

<script>
import "../../js/columns"
import DialogForm from './DialogForm.vue'
import { tableColmuns } from "./columns";
export default {
  components: {
    DialogForm,
  },
  data() {
    return {
      // 基本信息
      lr_isPage: true,

      // 查询参数
      queryHeight: 44,
      queryData: {
        projectName: "",
        constructionNature: "",
        constructionEntity: "",
        projectLevel: "",
        projectType: "",
      },

      queryItems: tableColmuns,

      // 表格参数
      tableLoading: false,
      tableData: [],
      tableTotal: 0,
      tableCurrentPage: 1,
      tablePageSize: 50,
      columns: tableColmuns,
      tableBtns: [
        { prop: 'Edit', label: '编辑' },
        { prop: 'CreateStage', label: '新建阶段', width: 60 },
      ],

      // 表单参数
      formVisible: false,
      formTitle: '',
      formEditRow: null,
      formEdit: false,

    }
  },
  computed: {
    consNatureOptions() { // 建设性质
      return this.lr_dataItemOptions(this.lr_dataItem['constructionNature'])
    },
    worksCategoryOptions() { // 工程类别
      return this.lr_dataItemOptions(this.lr_dataItem['worksCategory'])
    },
    projectLevelOptions() { // 项目等级
      return this.lr_dataItemOptions(this.lr_dataItem['projectLevel'])
    },

  },
  created() {

  },
  mounted() {
    this.lr_loadDataItem('constructionNature') // 建设性质
    this.lr_loadDataItem('projectLevel') // 项目等级
    this.lr_loadDataItem('trueOrFalse') // 是否
    this.lr_loadDataItem('worksCategory') // 工程类别
    this.lr_loadDataItem('subordinatePlate') // 所属区域
    this.lr_loadDataSourceData('businessStage', false) // 项目阶段业务

    this.init()
  },
  methods: {
    init() {
      this.loadTableData()
    },

    filterBtns(row, btns) {
      return btns
    },
    handleSearch() {
      this.loadTableData()
    },
    turnTablePage({ rows }) {
      this.tablePageSize = rows
      this.loadTableData(true)
    },
    async loadTableData(isNotFirst) {
      if (!isNotFirst) {
        this.tableCurrentPage = 1
      }
      this.tableLoading = true
      let queryData = this.$deepClone(this.queryData)
      queryData.rows = this.tablePageSize
      queryData.page = this.tableCurrentPage
      queryData.sidx = 'modifyTime'
      queryData.sord = 'DESC'
      const api = window.$crud("project");
      let data = await this.$awaitWraper(api.getPage(queryData))

      if (data != null) {
        this.tableData = data.rows
        this.tableTotal = data.records
      }
      else {
        this.tableTotal = 0
        this.tableData = []
      }
      this.tableLoading = false
    },
    /** 删除 */
    handleDelete($index, row) {
      this.$confirm(this.$t('是否删除该条数据?'), this.$t('提示'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(async () => {
        const api = window.$crud("project");
        await api.remove(row.id)
        this.$message({
          type: 'success',
          message: this.$t('删除成功!')
        })
        this.loadTableData()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: this.$t('已取消删除!')
        })
      })
    },

    handleDetails($index, row) {
      this.formEditRow = row
      this.formEditRow.projectId = row.id
      this.$refs.ProjectDetails.open(row)
    },

    handleEdit($index, row) {
      this.formEdit = true
      this.formDetail = false
      this.formEditRow = row
      this.$refs.DialogForm.open(row)
    },
    handleShowForm(text) {
      this.formTitle = text
      this.formVisible = true
    },
    async handleOpenedForm(showLoading, hideLoading) {
      if (this.formEdit) {
        showLoading('加载数据中...')
        await this.$refs.form.loadFormData(this.formEditRow.id)
        hideLoading()
      }
    },
    handleCloseForm() {
      this.$refs.form.resetForm()
      this.formVisible = false
    },

    /** 新增阶段 */
    handleCreateStage($index, row) {
      this.$refs.StageAdd.open({
        projectInfo: {
          id: row.id,
          projectId: row.id,
          projectName: row.projectName,
          projectCode: row.projectCode,
        }
      })
    },


  }
}
</script>
<style lang="scss" scoped></style>